package com.exwave.socialgame.model_society.service;

import com.exwave.socialgame.model_society.dao.CityteamsDao;
import com.exwave.socialgame.model_society.dao.CommentsDao;
import com.exwave.socialgame.model_society.entity.Comments;
import com.exwave.socialgame.myutils.WolfGameUtils;
import com.exwave.socialgame.myutils.ParamsException;

import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

import springfox.documentation.annotations.ApiIgnore;

/**
 * (Comments)表服务实现类
 *
 * @author makejava
 * @since 2021-12-14 17:14:38
 */
@ApiIgnore
@Service("commentsService")
public class CommentsServiceImpl implements CommentsService {
    @Resource
    private CommentsDao commentsDao;
    @Resource
    private CityteamsDao cityteamsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Comments queryById(Integer id) {
        WolfGameUtils.isIllegal(id); //判断id合法性
        Comments comments = this.commentsDao.queryById(id);
        WolfGameUtils.isNullOrEmpty(comments); //判断是否查询到
        return comments;
    }

    /**
     * 查询多条数据
     *
     * @param oid    帖子ID
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Comments> queryByOid(int oid, int offset, int limit) {
        WolfGameUtils.isNullOrEmpty(this.cityteamsDao.queryById(oid), "帖子不存在");
        WolfGameUtils.isIllegal(offset);
        WolfGameUtils.isIllegal(limit);
        List<Comments> comments = this.commentsDao.queryByOid(oid, offset, limit);
        WolfGameUtils.isListNull(comments, "查询失败");
        return comments;
    }


    /**
     * 新增数据
     *
     * @param comments 实例对象
     * @return 实例对象
     */
    @Override
    public Comments insert(Comments comments) {
        WolfGameUtils.isNullOrEmpty(comments.getContent());
        this.commentsDao.insert(comments);
        return comments;
    }

    /**
     * 修改数据
     *
     * @param comments 实例对象
     * @return 实例对象
     */
    @Override
    public Comments update(Comments comments) {
        this.commentsDao.update(comments);
        return this.queryById(comments.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        if (this.commentsDao.deleteById(id) <= 0)
            throw new ParamsException("帖子不存在");
        return true;
    }
}